package co.gov.medellin.ssti.sub.web.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.beans.factory.annotation.Autowired;

import co.gov.medellin.ssti.sub.common.excepcion.SUBException;
import co.gov.medellin.ssti.sub.model.service.ConsultasService;
import co.gov.medellin.ssti.sub.paoyer.administration.model.vo.UserVO;

public class AdicionInsertDominioAction extends Action {
	@Autowired
	ConsultasService consultasServiceImpl;

	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 * @return
	 * @throws java.io.IOException
	 * @throws javax.servlet.ServletException
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {

		try {
			HttpSession session = request.getSession();
			// Proceso para la auditoria
			UserVO user = (UserVO) session.getAttribute("userInfo");
			String terminalAddress = request.getRemoteAddr();
			String strTerminal = ("WEB=" + terminalAddress);
			Calendar c = Calendar.getInstance();
			SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
			// Fin del proceso

			String Opcion = (String) session.getAttribute("Escoger");

			if (Opcion.equals("1")) {
				session.setAttribute("Escoger", "1");
			} else {
				session.setAttribute("Escoger", "2");
			}
			String strDom = request.getParameter("dom");
			int numero = Integer.parseInt(strDom.trim());

			if (numero != 2) {
				String CodAtri = request.getParameter("codatributo");
				session.setAttribute("codatributo", CodAtri);
			}

			String strCodAtri = (String) session.getAttribute("codatributo");

			if (numero != 2) {

				String strTipoVflDescrip = request
						.getParameter("tipovfldescrip");
				String strTipoVflVigente = request
						.getParameter("tipovflvigente");
				String strTipoVflNombre = strTipoVflDescrip + strCodAtri;

				String strArrDo[] = { strTipoVflNombre, strTipoVflDescrip,
						strTipoVflVigente, user.getName(),
						dateFormat.format(c.getTime()), strTerminal,
						user.getName(), dateFormat.format(c.getTime()),
						strTerminal };

				int bInsertTipoVFL = consultasServiceImpl
						.insertTipoVFL(strArrDo);
				int iCodigoMax = consultasServiceImpl.findCodigoTipoVFL();
				String strArrAct[] = { user.getName(),
						dateFormat.format(c.getTime()), strTerminal };
				int ActualizoBien = consultasServiceImpl.actualizarAtributo(
						strCodAtri, iCodigoMax, strArrAct);

				// PROCESO PARA CALCULAR Y ACTUALIZA EL ATRIBUTO GENERICO
				int iCodAtriGenerico = consultasServiceImpl
						.findCodAtriEspe(strCodAtri);
				int ActualizoBienGene = consultasServiceImpl
						.actualizarAtributoGene(iCodAtriGenerico, iCodigoMax,
								strArrAct);
				// FIN PROCESO
			}

			int iCodigoMax;
			if (Opcion.equals("1")) {
				iCodigoMax = consultasServiceImpl.findCodigoTipoVFL();
			} else {
				String strCodTVFL = (String) session
						.getAttribute("tipovalorflex");
				iCodigoMax = Integer.parseInt(strCodTVFL.trim());
			}

			for (int i = 1; i < 8; i++) {

				String strDescripcion = request.getParameter("descripcion" + i);
				String strCodigo = request.getParameter("codigo" + i);
				String Nombre = strDescripcion + strCodAtri;
				String strArr[] = { Nombre, strDescripcion, user.getName(),
						dateFormat.format(c.getTime()), strTerminal,
						user.getName(), dateFormat.format(c.getTime()),
						strTerminal, strCodigo };
				if (!strDescripcion.equalsIgnoreCase("")) {
					int bInsertVFL = consultasServiceImpl.insertValorVFL(
							strArr, iCodigoMax);
				}
			}
		} catch (Exception e) {
			SUBException.logDebugError("", e);
		}

		return mapping.findForward("success");
	}
}